// pages/ArticleDetails/ArticleDetails.js
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: app.data.imageUrl,
    data: {},
    id: 0,
    is_sc: 0,

    config: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getConfig(this);
    this.loadData();
  },
  /**
   * 加载数据
   */
  loadData: function () {
    var that = this;
    var url = app.data.apiUrl + 'Index/docinfo';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      id: that.data.id
    };
    help.get(url, data, that, function (res, that) {
      var result = res.data;
      // console.log(result);
      if (result.code == 1) {
        that.setData({
          data: result.data,
          is_sc: result.data.yisheng !== null && result.data.yisheng.is_sc == 1 ? 1 : 0 
        })
        wx.setNavigationBarTitle({
          title: result.data.title
        })
        WxParse.wxParse('content', 'html', result.data.content, that, 5);
      } else if (result.code == 1) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },
  //关注
  guanzhu: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    
    var url = app.data.apiUrl + '/Yisheng/sc';
    wx.showLoading({
      title: '正在加载...',
    })
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      id: id,
      doc_id: that.data.data.id
    };
    help.get(url, data, that, function (res, that) {
      wx.hideLoading()
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        if (that.data.is_sc == 1) {
          help.msg('取消关注成功');
          that.setData({
            is_sc: 0
          })
        } else {
          help.msg('关注成功');
          that.setData({
            is_sc: 1
          })
        }

      } else if (result.code == 1) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
      } else {
        help.msg(result.msg)
        return false;
      }


    }, function (res, that) {
      wx.hideLoading()
      console.log(res);
      help.msg('网络异常！')
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})